package com.faintlight;

import com.faintlight.config.WxMpProperties;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

@SpringBootApplication
@EnableConfigurationProperties(WxMpProperties.class)
public class FaintLightApplication {

    public static void main(String[] args) {
        SpringApplication.run(FaintLightApplication.class, args);
        System.out.println("\n" +
                "  ╔════════════════════════════════════════════════════╗\n" +
                "  ║  ✨ 萤窗温暖启动成功！所有孤独都将得到温柔的回声 ║\n" +
                "  ╠════════════════════════════════════════════════════╣\n" +
                "  ║  • 技术不是温暖的创造者，而是它的翻译官          ║\n" +
                "  ║  • 访问 http://localhost:8080/api/swagger-ui.html  ║\n" +
                "  ║    查看API文档                                    ║\n" +
                "  ╚════════════════════════════════════════════════════╝\n");
    }

    // ✅ 新增：全局 CORS Filter
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>();
        CorsFilter corsFilter = new CorsFilter();
        registrationBean.setFilter(corsFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        return registrationBean;
    }

    // ✅ 自定义 CorsFilter 类
    public static class CorsFilter implements Filter {
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;

            // 允许的来源
            String origin = request.getHeader("Origin");
            if (origin != null && (
                    origin.equals("http://localhost:63342") ||
                    origin.equals("http://localhost:5500") ||
                    origin.equals("http://127.0.0.1:63342") ||
                    origin.equals("http://localhost:8080") ||
                    origin.equals("file://")
            )) {
                response.setHeader("Access-Control-Allow-Origin", origin);
            }

            // 允许的请求方法
            response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            // 允许的请求头
            response.setHeader("Access-Control-Allow-Headers", "*");
            // 允许携带 Cookie
            response.setHeader("Access-Control-Allow-Credentials", "true");
            // 预检请求缓存时间
            response.setHeader("Access-Control-Max-Age", "3600");

            // 处理预检请求
            if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
                return;
            }

            chain.doFilter(req, res);
        }

        @Override
        public void init(FilterConfig filterConfig) {}

        @Override
        public void destroy() {}
    }
}
